package cn.iocoder.yudao.module.bpm.service.oaleavapply;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.bpm.controller.admin.oaleavapply.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaleavapply.OaLeavApplyDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaleavapplysub.OaLeavApplySubDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.bpm.dal.mysql.oaleavapply.OaLeavApplyMapper;
import cn.iocoder.yudao.module.bpm.dal.mysql.oaleavapplysub.OaLeavApplySubMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 申请单 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class OaLeavApplyServiceImpl implements OaLeavApplyService {

    @Resource
    private OaLeavApplyMapper oaLeavApplyMapper;
    @Resource
    private OaLeavApplySubMapper oaLeavApplySubMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createOaLeavApply(OaLeavApplySaveReqVO createReqVO) {
        // 插入
        OaLeavApplyDO oaLeavApply = BeanUtils.toBean(createReqVO, OaLeavApplyDO.class);
        oaLeavApplyMapper.insert(oaLeavApply);

        // 插入子表
        createOaLeavApplySubList(oaLeavApply.getId(), createReqVO.getOaLeavApplySubs());
        // 返回
        return oaLeavApply.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOaLeavApply(OaLeavApplySaveReqVO updateReqVO) {
        // 校验存在
        validateOaLeavApplyExists(updateReqVO.getId());
        // 更新
        OaLeavApplyDO updateObj = BeanUtils.toBean(updateReqVO, OaLeavApplyDO.class);
        oaLeavApplyMapper.updateById(updateObj);

        // 更新子表
        updateOaLeavApplySubList(updateReqVO.getId(), updateReqVO.getOaLeavApplySubs());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOaLeavApply(Long id) {
        // 校验存在
        validateOaLeavApplyExists(id);
        // 删除
        oaLeavApplyMapper.deleteById(id);

        // 删除子表
        deleteOaLeavApplySubByFk(id);
    }

    private void validateOaLeavApplyExists(Long id) {
        if (oaLeavApplyMapper.selectById(id) == null) {
            throw new ServiceException(500, "请假申请不存在");
        }
    }

    @Override
    public OaLeavApplyDO getOaLeavApply(Long id) {
        return oaLeavApplyMapper.selectById(id);
    }

    @Override
    public PageResult<OaLeavApplyDO> getOaLeavApplyPage(OaLeavApplyPageReqVO pageReqVO) {
        return oaLeavApplyMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（请假申请） ====================

    @Override
    public List<OaLeavApplySubDO> getOaLeavApplySubListByFk(Long fk) {
        return oaLeavApplySubMapper.selectListByFk(fk);
    }

    private void createOaLeavApplySubList(Long fk, List<OaLeavApplySubDO> list) {
        list.forEach(o -> o.setFk(fk));
        oaLeavApplySubMapper.insertBatch(list);
    }

    private void updateOaLeavApplySubList(Long fk, List<OaLeavApplySubDO> list) {
        deleteOaLeavApplySubByFk(fk);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createOaLeavApplySubList(fk, list);
    }

    private void deleteOaLeavApplySubByFk(Long fk) {
        oaLeavApplySubMapper.deleteByFk(fk);
    }

}
